Jupyter Notebook ExtensionsΒΆ

A wide variety of extensions are available that can be used to customise the Jupyter notebook environment. Customisations include the ability to add notebook toolbar or menu options and customise the behaviour of notebook cells.

Several extensions are pre-enabled in the TM351 virtual machine, as described below.

Notebook extensions can be managed via the nbextensions_configurator, which can be found via the tab on the notebook homepage or from within a notebook via the Edit->nbextensions config menu option.

Notebook Extension ConfiguratorΒΆ

The notebook extension configurator allows you to enable or disable extensions by checking the corresponding select box. Extensions are versioned and an option exists to disable activation of an extension from the configurator if it is not explicilty declared as compatible with the current notebook. In many cases, these extensions will still work, so I tend to allow all extensions to be enabled and then just ignore any that cause me problems with a particular notebook.

If you click on the name of an extension, a descriptive panel appears that describes that extension. This panel contains two sections:

  • a parameter section
  • a README section

The parameter section allows you to customise the settings of the extension itself:

The README section describes the operation of the extension:

Pre-Enabled ExtensionsΒΆ

The TM351 VM is bundled with the "official" ipython-contrib/jupyter_contrib_nbextensions as well as several other custom extensions including extensions developed to support OU related activities.

These extensions may be disabled from the notebook extension configurator, although the notebooks may not displayed as intended if you do disable them.

General Notebook PresentationΒΆ

  • collapsible_headings: provides upport for collapsible heading so that hierarchically organised sections of the notebook appearing in separate markdown cells can be collapsed or expanded.

The Table of Contents (2) (sic) extension (not enabled by default) can also use headings to create a table of contents that appears at the top of the notebook (check the appropriate setting).

The Code Font Size extension (not enabled by default) allows you to increase or decrease the font size of code cells or the contents of markdown cells in edit mode.

Workflow SupportΒΆ

  • jupyter_wysiwyg: provides an optional rich text, WYSIWYG editor for editing markdown cells; enabled via a button that appears on the left hand side of a markdown cell in edit mode.
  • highlighter: highlight selected text in a markdown cell
  • spellchecker: performs a live spell check as you edit the contents of a markdown cell
  • export_embedded: adds an export option to the File menu that allows you to export the current notebook as an HTML file with images embedded in it (File -> Download as -> HTML Embedded (.html))
  • freeze: allows you to make a cell read-only / non-editable or frozen (read-only and non-executable if it's a code cell)
InΒ [Β ]:
print('Read only code cell')
InΒ [Β ]:
print('Frozen code cell')
  • init_cell: allows one or more cells to set as initialisation cells that can be executed from the toolbar or autmomatically whenever the notebook kernel is (re)started.
  • skip-traceback: collapse Python error messages under a succinct statement of the error.
InΒ [14]:
pront("What happens if you run this cell, as is...?")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-14-d130e3700c65> in <module>()
----> 1 pront("Did I type that wrong???")

NameError: name 'pront' is not defined
  • codefolding: allow code folding on function names, first line comments and cell magics
InΒ [5]:
def collapsed_function1():
    pass

def uncollapsed_function():
    pass

def collapsed_function2():
    pass
InΒ [Β ]:
# First line comment lets you collapse a code cell in its entirety
pass
  • hide_input: allows the output of a specific code cell to be hidden. For example, select the (hidden) code cell directly beneath cell and see if you can reveal it.

If you run the cell while it is hidden, any output will be displayed. You can also select a hidden code cell and run it to see if it renders any output.

InΒ [13]:
# Found it!
print('Hello... The code that produced me is (or at least, was) hidden...')
Hello... The code that produced me is (or at least, was) hidden...
  • python-markdown: allows the value of python variables to be returned inline into a markdown cell

Some markdown with a sum in it: {{a=1;a}} + {{a}} = {{a+a}}. Put this markdown cell into cell edit mode to see the code...

It works with pandas too...

InΒ [10]:
import pandas as pd
df = pd.DataFrame({'col1':['a','b','c'],'col2':[1,2,3] })

Here's my table...

{{df}}

Although not directly relevant to notebook extensions, it's also worth noting that pandas dataframes can be styled as per pandas docs - Styling.

Module Team / AL SupportΒΆ

  • rubberband: supports the click'n'drag selection of multiple contiguous cells for use with the exercise2 extension
  • exercise2: from a group of selected cells, use the first cell as a question cell and hide the selected cell(s) below; also add a "reveal answer" button to the first cell in the selected group to toggle the display of the answer cell(s)
  • RISE: this extension turns a notebook with appropriately set cell metadata into a "live slideshow", with cells appearing as slides or slide fragments. Code cells are executable within the slideshow. Any output returned from an executed code cell will also be rendered within the slide. For more detail, see the official docs.

OU custom menu extensionsΒΆ

As well as the notebook toolbar extensions, two custom OU extensions have been added to the File menu.

  • MS Word export: download the current notebook as a Microsoft Word document (File -> Download as -> MS Word (.docx))
  • ODS : download the current notebook as an HTML file and compress it with the original (saved) .ipynb document as a zip file using an .nbk suffix so that it can be uploae to Open Design Studio (File -> ODSzip (.nbk))